/**
  ******************************************************************************
  * @file    set_timer.c
  * @author  Hanif Rizal
  * @version V1.0.0
  * @date    03-Maret-2013
  * @brief   ATXmega timer access routine
  *              
  *  @verbatim
  *  
  *          ===================================================================
  *                                 How to use this driver
  *          ===================================================================              
  *   
  *  @endverbatim
  *    
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2013 Vibration Monitoring System TEAM</center></h2>
  ******************************************************************************
  */
#include "set_timer.h"

volatile unsigned char timD1Cnt=0;
volatile unsigned char timD1CntFlag=0;

void myTimerInit(void)
{
	TCD0.PER = 65535;	//Set Periode Max
}

void myTimerEventInit(void)
{
	//TCD0.PER = 16000;	//Set Periode 0.5 detik
	TCD0.PER = 1600;	//Set Periode 0.05 detik
	EVSYS.CH7MUX = EVSYS_CHMUX_TCD0_OVF_gc; //Konfigurasi TimerD Channel 0 Overvlow event sebagai even generator di CH7 Event System
}

void myTimerStart(void)
{
	TCD0.CTRLA = TC_CLKSEL_DIV1024_gc; // ftimer  = 32MHz (fPER) / 1024 = 32KHz
}

int myTimerStop(void)
{
	int temp;
	
	TCD0.CTRLA = TC_CLKSEL_OFF_gc; // Stop Timer
	temp = TCD0.CNT;
	TCD0.CNT = 0x0000;
	
	return temp;
}

void myTimer1Init(unsigned char sec)
{
	timD1Cnt=sec;
	timD1CntFlag=0;
	TCD1.PER = 50000;	//Set Periode 1 Sec
	TCD1.INTCTRLA = TC_OVFINTLVL_LO_gc;	//Enable interrupt overflow
}

void myTimer1Start(void)
{
	//TCD1.CTRLA = TC_CLKSEL_DIV1024_gc; // ftimer  = 32MHz (fPER) / 1024 = 32KHz;
	TCD1.CTRLA = TC_CLKSEL_DIV64_gc; // ftimer  = 32MHz (fPER) / 64 = 500KHz;
}

void myTimer1Stop(void)
{
	TCD1.CTRLA = TC_CLKSEL_OFF_gc; // Stop Timer
	TCD1.CNT = 0x0000;
}